<?php

namespace plugin\ycMassage\app\shop\controller;

use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageShop;
use support\Request;

class ShopOrdersController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addScreen('prop', 'select', '字段', [
            'options' => [
                [
                    'value' => 'person_time',
                    'label' => '人次'
                ],
                [
                    'value' => 'orders_num',
                    'label' => '订单量'
                ],
                [
                    'value' => 'orders_price',
                    'label' => '订单金额(默认)'
                ],
                [
                    'value' => 'duration',
                    'label' => '服务时长'
                ]
            ]
        ]);
        $builder->addScreen('sort', 'select', '排序', [
            'options' => [
                [
                    'value' => 'asc',
                    'label' => '正序'
                ],
                [
                    'value' => 'desc',
                    'label' => '倒序(默认)'
                ]
            ]
        ]);
        $builder->addScreen('date', 'select', '时间', [
            'options' => [
                [
                    'value' => 'month',
                    'label' => '本月(默认)'
                ],
                [
                    'value' => 'last_month',
                    'label' => '上月'
                ]
            ]
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addColumn('', '名次', [
            'width' => 90,
            'type' => 'seq'
        ]);
        $builder->addColumn('id', 'ID', [
            'width' => 90
        ])
            ->addColumnEle('logo', 'Logo', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('title', '门店', [
                'width' => 120,
            ]);
        $builder->addColumn('person_time', '人次', []);
        $builder->addColumn('orders_num', '订单量', []);
        $builder->addColumn('orders_price', '订单额', []);
        $builder->addColumn('duration', '服务时长(分钟)', []);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $where = [
            ['t.saas_appid', '=', $request->saas_appid]
        ];
        if ($request->use_mch_id) {
            $where[] = ['t.mch_id', '=', $request->use_mch_id];
        } else {
            return $this->fail('参数错误');
        }
        $MassageShop = MassageShop::where($where)
            ->alias('t')
            ->join('massage_shop_statistics s', "s.shop_id = t.id", 'LEFT')
            ->group('t.id')
            ->field('t.id,t.logo,t.title,SUM(s.person_time) as person_time,SUM(s.orders_num) as orders_num,SUM(s.orders_price) as orders_price,SUM(s.duration) as duration,s.create_time');
        $date = $request->get('date', 'month');
        if ($date == 'last_month') {
            $MassageShop->whereMonth('s.create_time', 'last month');
        } else {
            $MassageShop->whereMonth('s.create_time');
        }
        $prop = $request->get('prop', 'orders_price');
        $sort = $request->get('sort', 'desc');
        if ($prop) {
            $order = $prop . ' ' . $sort;
        }
        $MassageShop->order($order);
        $data = $MassageShop->select()->each(function ($item) {
            if (strpos($item->logo, 'http') !== 0) {
                $item->logo = UploadService::url($item->logo);
            }
        });
        return $this->successRes(['data' => $data]);
    }
}
